<?php
namespace app\model;
use app\BaseModel;
use think\model\concern\SoftDelete;
use app\model\Member;

class CompanyBalance extends BaseModel
{

    use SoftDelete;
    protected $deleteTime = 'delete_time';
    protected $defaultSoftDelete = '0000-00-00 00:00:00';
    protected $hidden = ['update_time','delete_time','sort'];
    // 定义全局的查询范围
    protected $globalScope = ['status'];

    public function scopeStatus($query)
    {
        $query->where('status',2);
    }
    public function searchStartTimeAttr($query,$value, $data)
    {

        if (!empty($value)) {
            $query->where('create_time', '>=', $value);
        }
    }

    public function searchEndTimeAttr($query, $value, $data)
    {
        if (!empty($value)) {
            $query->where('create_time', '<=', $value);
        }
    }

    public function searchCompanyIdAttr($query, $value, $data)
    {
        if (!empty($value)) {
            $query->where('company_id', '=', $value);
        }
    }

    protected function setCompanyIdAttr($value, $data)
    {
        //当前字段的修改器
        $member = LeasingCompany::where('id', $value)->find();
        $this->set('company_id', $member['mobile']);
        $this->set('company_name', $member['title']);
        return $value;
    }
    /*
     * +充值
     * -提现
     */
    public static function saveData($member_id, $mark, $price, $order_number, $payment)
    {
        if ($mark == '+') {
            self::create([
                'mark' => $mark,
                'company_id' => $member_id,
                'price' => $price,
                'order_number' => $order_number,
                'payment' => $payment
            ]);
            CompanyBalance::where('id', $member_id)->inc('balance', $price)->update();
        }
        if ($mark == '-') {
            self::create([
                'mark' => $mark,
                'company_id' => $member_id,
                'price' => $price,
                'order_number' => $order_number,
                'payment' => $payment
            ]);
            CompanyBalance::where('id', $member_id)->dec('balance', $price)->update();
        }

    }



}